local Msg = require("core.Msg")
local PlayExcel = require("excel.ComplexSrvActivity").activityPlay
local Grid = require("bag.Grid")
local ActDefine = require("activity.Define")
local BagLogic = require("bag.BagLogic")
local Broadcast = require("broadcast.Broadcast")
local Lang = require("common.Lang")
local ComplexSrvActFL = require("activity.ComplexSrvActFL")
local HtmlUtil = require("common.HtmlUtil")
local Log = require("common.Log")
local ActivityDaiExcel = require("excel.Activity").dailyAct
local DailyActFL = require("activity.DailyActFL")
local CopyCommon = require("copy.CopyCommon")

function getActivityName(id)
  for k,v in pairs(ActivityDaiExcel) do
    if k == id then
      return v.name
    end
  end
  return ""
end

function getActivityType(id)
  for k,v in pairs(ActivityDaiExcel) do
    if k == id then
      return v.actType
    end
  end
  return 0
end

function checkOpenToday(nWday, actId)
  for k,v in pairs(ActivityDaiExcel[actId].dayWed) do
    if nWday == k and v == 1 then
      return false
    end
  end
  return true
end

function checkActOpen(actId)  
  local d = os.date("*t", os.time())
  local nWday = (d.wday + 5) % 7 + 1
  if next(ActivityDaiExcel[actId].dayWed) == nil then
    return true
  end
  return checkOpenToday(nWday, actId)
end

function open(human, id, type)
  local sendMsg = Msg.gc.GC_OPEN_COMPLEX_PAYDRAW_ACTIVITY
  local activityNum = ComplexSrvActFL.onGetCurActRuleID(id)
  local activity = PlayExcel[activityNum]
  if not activity then
    return
  end
  local len = 0  
  local cache = {}
  for i=1, #activity.condition do
    if checkActOpen(activity.condition[i].actId) then   
      len = len + 1   
      sendMsg.info[len].reward[0] = #activity.reward[i]
      for j=1, #activity.reward[i] do
        Grid.create(cache, activity.reward[i][j][1], activity.reward[i][j][2])
        Grid.makeItem(cache, sendMsg.info[len].reward[j], 0, human)
      end
      sendMsg.info[len].args[0] = 2
      sendMsg.info[len].args[1] = activity.images[i]
      sendMsg.info[len].args[2] = i
      sendMsg.info[len].canGet = human.db.YYActivityPlayIsGet[i - 1]
      sendMsg.info[len].name = getActivityName(activity.condition[i].actId)
    end
  end
  sendMsg.info[0] = len
  sendMsg.myInfo[0] = 1
  sendMsg.myInfo[1] = human.db.dailyChongzhi
  sendMsg.activityId = id 
  sendMsg.type = type or 1 
  sendMsg.rewardInfo[0] = 0
  Msg.send(sendMsg, human.fd)
end
--领取奖励
function getReward(human, id, index)  
  if human.db.YYActivityPlayIsGet[index - 1] ~= ActDefine.REWARD_CAN_GET then
    return
  end
  local activityNum = ComplexSrvActFL.onGetCurActRuleID(id)
  local activity = PlayExcel[activityNum]
  if not activity then
    return
  end
  if not BagLogic.checkCanAddItemList(human, activity.reward[index], #activity.reward[index]) then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.YUNYING_BAG_NOT_ENOUGH)
  end
  
  for i=1, #activity.reward[index] do
    BagLogic.addItem(human, activity.reward[index][i][1], 1, activity.reward[index][i][2], "complexactholidaygift")
  end
  local str = string.format(Lang.COMPLEX_CRIT_ACTIVITY_PLAY, HtmlUtil.fontName(human.name, human._id), human._id)
  Broadcast.sendBroadcast2World(Lang.UP + Lang.GROSSIP, str)
  human.db.YYActivityPlayIsGet[index - 1] = ActDefine.REWARD_ALREADY_GET
  open(human, id)
end

function checkCanGet(human, copyId)
  local activityNum = ComplexSrvActFL.onGetCurActRuleID(ActDefine.OPERATEACT_TYPE_ACTIVITYPLAY)
  local activity = PlayExcel[activityNum]
  if not activity then
    return
  end
  
  for i=1, #activity.condition do
    if copyId == 416 then
      if DailyActFL.onCheckWhichBossIsRunning() == activity.condition[i].actId and human.db.YYActivityPlayIsGet[i - 1] == ActDefine.REWARD_NOT_CAN_GET then
        human.db.YYActivityPlayIsGet[i - 1] = ActDefine.REWARD_CAN_GET
        open(human, ActDefine.OPERATEACT_TYPE_ACTIVITYPLAY, 1)
      end
    elseif copyId == activity.condition[i].copyId and human.db.YYActivityPlayIsGet[i - 1] == ActDefine.REWARD_NOT_CAN_GET then
      human.db.YYActivityPlayIsGet[i - 1] = ActDefine.REWARD_CAN_GET
      open(human, ActDefine.OPERATEACT_TYPE_ACTIVITYPLAY, 1)
    end
  end  
end

function checkAllCanGet(human)
  local activityNum = ComplexSrvActFL.onGetCurActRuleID(ActDefine.OPERATEACT_TYPE_ACTIVITYPLAY)
  local activity = PlayExcel[activityNum]
  if not activity then
    return
  end
  for i=1, #activity.condition do
    if activity.condition[i].copyId ~= 416 then
      if CopyCommon.getCopyEnterCount(human, activity.condition[i].copyId) >= 1 and human.db.YYActivityPlayIsGet[i - 1] == ActDefine.REWARD_NOT_CAN_GET then
        human.db.YYActivityPlayIsGet[i - 1] = ActDefine.REWARD_CAN_GET
        open(human, ActDefine.OPERATEACT_TYPE_ACTIVITYPLAY, 1)
      end
    end
  end
end
